//package com.naiterui.ehp.bp.support.security;
//
//import java.util.Optional;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import com.naiterui.ehp.bp.support.context.RequestContext;
//import com.naiterui.ehp.bp.support.utils.JwtHelper;
//import com.naiterui.ehp.bp.support.utils.JwtUser;
//
///**
// * @author gaoliangliang
// * @date 2019-07-09 16:08
// * @since 1.0.0
// */
//public class CommonInterceptor implements HandlerInterceptor {
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//
//        Long doctorId = Optional.ofNullable(request.getParameter(SettingConfig.DOCTOR_ID)).map(Long::valueOf).orElse(null);
//        Long patientId = Optional.ofNullable(request.getParameter(SettingConfig.PATIENT_ID)).map(Long::valueOf).orElse(null);
//        Long doctorIdHeader = Optional.ofNullable(request.getHeader(SettingConfig.DOCTOR_ID)).map(Long::valueOf).orElse(null);
//        Long patientIdHeader = Optional.ofNullable(request.getHeader(SettingConfig.PATIENT_ID)).map(Long::valueOf).orElse(null);
//        Integer origin = Optional.ofNullable(request.getHeader(HeaderConstant.HEADER_ORIGIN)).map(Integer::valueOf).orElse(-1);
//        doctorId = doctorId == null ? doctorIdHeader : doctorId;
//        patientId = patientId == null ? patientIdHeader : patientId;
//
//        if (HeaderConstant.HEADER_ORIGIN_PATIENT_WX == origin) {
//            String token = request.getHeader(SettingConfig.TOKEN);
//            JwtUser jwtUser = JwtHelper.verifyToken(token);
//            patientId = jwtUser == null ? patientId : jwtUser.getUserId();
//        }
//
//        RequestContext.set(RequestContext
//                .builder()
//                .doctorId(doctorId)
//                .patientId(patientId)
//                .origin(request.getHeader(HeaderConstant.HEADER_ORIGIN))
//                .platform(request.getHeader(HeaderConstant.HEADER_PLATFORM))
//                .version(request.getHeader(HeaderConstant.HEADER_VERSION))
//                .h5native(request.getHeader(HeaderConstant.HEADER_NATIVE))
//                .h5nativeVersion(request.getHeader(HeaderConstant.HEADER_NATIVE_VERSION))
//                .build());
//
//        return true;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        RequestContext.clear();
//    }
//}
